package com.gitee.wsl.func.factory.sample

import com.gitee.wsl.func.factory.Factory


/**
 * Factory implementation that always throws an exception.
 *
 * @param <T> the type of results supplied by this supplier.
 * @since 3.0
</T> */
class ExceptionFactory<T>
/**
 * Restricted constructor.
 */
private constructor() : Factory<T> {
    /**
     * Always throws an exception.
     *
     * @return never
     * @throws FunctorException always
     */
    override fun create(): T {
        throw FunctorException("ExceptionFactory invoked")
    }

    /**
     * Returns the singleton instance.
     *
     * @return the singleton instance.
     */
    private fun readResolve(): Any {
        return INSTANCE
    }

    companion object {

        /** Singleton predicate instance  */
        // the static instance works for all types
        val INSTANCE = ExceptionFactory<Nothing>()

        /**
         * Factory returning the singleton instance.
         *
         * @param <T>  the type the factory creates
         * @return the singleton instance
         * @since 3.1
        </T> */
        fun <T> exceptionFactory(): Factory<T> {
            return INSTANCE as Factory<T>
        }
    }
}

class FunctorException(string: String,throwable: Throwable? = null): Exception(string,throwable) {
    constructor(throwable: Throwable):this("",throwable)
}
